<!--
 * @Description: 引入shell
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-02-02 11:21:10
 * @LastEditTime: 2021-08-04 14:31:51
 * @LastEditors: 0001100986/LiuShuai
-->
<template>
  <div class="ssh-box">
    <el-button @click="cancel">返回</el-button>
    <telnet
      ref="telnetSSH"
      :type="type"
      @on-watchingHtml="watchingHtml"
      style="height:100%; overflow: hidden"
    />
  </div>
</template>

<script>
import telnet from './telnet';
export default {
  name: 'shell',
  components: { telnet },
  props: ['data', 'type'],
  data () {
    return {};
  },
  created () {
    this.onConnect('first');
  },
  methods: {
    // 返回
    cancel () {
      let _this = this;
      _this.$emit('handleChange', 0);
    },
    // 监听html向vue发起的信息
    watchingHtml (res) {
      if (res) {
        if (res.type) {
          this[res.type](res.value);
        }
      }
    },
    async onConnect (flag) {
      try {
        let param = {
          neId: this.data.id,
          loginType: this.data.telnetConfig.ssh ? 'ssh' : 'telnet',
          hostname: this.data.ipAddress, // todos
          username: this.data.authInfo.username,
          password: this.data.authInfo.password,
          port: this.data.telnetConfig.port,
          enPasswordPrompt: this.data.telnetConfig.enPasswordPrompt,
          enableCommand: this.data.telnetConfig.enableCommand,
          enablePassword: this.data.telnetConfig.enablePassword,
          enablePrompt: this.data.telnetConfig.enablePrompt,
          loginPrompt: this.data.telnetConfig.loginPrompt,
          passwordPrompt: this.data.telnetConfig.passwordPrompt,
          userPrompt: this.data.telnetConfig.userPrompt
        };
        this.$nextTick(() => {
          this.$refs.telnetSSH.dealLoading({
            type: 'loading',
            hostname: param.hostname
          });
        });
        const result = await this.$http.post({
          url: '/api/telnetssh/manage/login-connection-id',
          data: param
        });
        if (result) {
          let passByObjects = {
            type: 'reconnect',
            flag: flag,
            connectionId: result.id,
            hostname: this.data.ipAddress,
            port: result.websocketPort,
            websocketHost: window.location.host,
            scheme: window.location.origin.startsWith('https') ? 'wss' : 'ws'
          };
          await this.$refs.telnetSSH.reconnect(passByObjects);
        }
      } catch (err) {
        this.$message.error(err);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.ssh-box {
  ::v-deep .el-button {
    margin-bottom: 15px;
  }
}
</style>